﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Assembly
{
    public partial class frm_Edit_AssemblyCheckType : Form
    {
        public string id;
        public frm_Edit_AssemblyCheckType()
        {
            InitializeComponent();
            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);
            this.id = null;
        }         

        /// <summary>
        /// Load Data
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public bool LoadData()
        {
            bool ret = true;
            if (string.IsNullOrEmpty(this.id))
            {
                ret = false;
            }
            else
            {
                try
                {
                    myConnection.Open();
                    SqlCommand sql_cmd = myConnection.CreateCommand();
                    sql_cmd.CommandText = "SELECT * FROM [231_CheckType] WHERE CHeckTypeID = " + this.id;
                    SqlDataReader dataReader = sql_cmd.ExecuteReader();
                    if (dataReader.Read())
                    {
                        this.checkTypeName.Text = dataReader["CheckTypeName"].ToString().Trim();
                    }
                    dataReader.Close();
                    sql_cmd.Dispose();
                }
                catch
                {
                    ret = false;
                }
                finally
                {
                    myConnection.Close();
                }
            }
            return ret;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }        
        
        ///////////////////////////
        /// When Click Add Button
        ///////////////////////////        
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            string err = null;
            string checktypename = null;           

            if (string.IsNullOrEmpty(err))
            {
                if (string.IsNullOrEmpty(this.checkTypeName.Text.Trim()))
                {
                    err = "Please enter Check Type Name";
                }
                else
                {
                    checktypename = this.checkTypeName.Text.Trim();
                }
            }

            if (string.IsNullOrEmpty(err))
            {
                try
                {
                    myConnection.Open();
                    SqlCommand sql_cmd = myConnection.CreateCommand();
                    sql_cmd.CommandText = "UPDATE [231_CheckType] SET CheckTypeName = N'" + checktypename + "'"
                        + " WHERE CheckTypeID = " + this.id;
                    sql_cmd.ExecuteNonQuery();
                    sql_cmd.Dispose();
                    MessageBox.Show("Update Check Type successfully.");
                }
                catch
                {
                    MessageBox.Show("Cannot update Check Type");
                }
                finally
                {
                    myConnection.Close();
                }
            }
            else
            {
                MessageBox.Show(err);
            }
        }
    }
}
